package com.css.zfzx.sjcj.modules.qhseismicfortification.repository.impl;

import com.css.zfzx.sjcj.modules.qhseismicfortification.repository.QhSeismicFortificationNativeRepository;
import com.css.zfzx.sjcj.modules.qhseismicfortification.repository.entity.QhSeismicFortificationEntity;
import com.css.zfzx.sjcj.modules.qhseismicfortification.viewobjects.QhSeismicFortificationQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * @author tao
 * @date 2020-11-17
 */
@Repository
public class QhSeismicFortificationNativeRepositoryImpl implements QhSeismicFortificationNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(QhSeismicFortificationNativeRepositoryImpl.class);


    @Override
    public Page<QhSeismicFortificationEntity> queryQhSeismicFortifications(QhSeismicFortificationQueryParams queryParams, int curPage, int pageSize) {
        if(PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            queryParams.setIsValid("1");
        }
        StringBuilder sql = new StringBuilder("select * from qh_seismic_fortification");
        StringBuilder whereSql = new StringBuilder(" where 1=1");
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getTown())) {
            whereSql.append(" and town like :town");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getProjectName())) {
            whereSql.append(" and project_name = :projectName");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDrillCode())) {
            whereSql.append(" and drill_code like :drillCode");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getQualityinspectionResult())) {
            whereSql.append(" and qualityinspection_result = :qualityinspectionResult");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getQualityinspectionStatus())) {
            whereSql.append(" and qualityinspection_status = :qualityinspectionStatus");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            whereSql.append(" and is_valid = :isValid");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDrillCodes())) {
            whereSql.append(" and drill_code in :drillCodes");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhSeismicFortificationEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from qh_seismic_fortification");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
            countQuery.setParameter("city", queryParams.getCity());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
            countQuery.setParameter("area", queryParams.getArea());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getTown())) {
            query.setParameter("town", "%" + queryParams.getTown() + "%");
            countQuery.setParameter("town", "%" + queryParams.getTown() + "%");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getProjectName())) {
            query.setParameter("projectName", queryParams.getProjectName());
            countQuery.setParameter("projectName", queryParams.getProjectName());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDrillCode())) {
            query.setParameter("drillCode", "%" + queryParams.getDrillCode() + "%");
            countQuery.setParameter("drillCode", "%" + queryParams.getDrillCode() + "%");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getQualityinspectionResult())) {
            query.setParameter("qualityinspectionResult", queryParams.getQualityinspectionResult());
            countQuery.setParameter("qualityinspectionResult", queryParams.getQualityinspectionResult());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getQualityinspectionStatus())) {
            query.setParameter("qualityinspectionStatus", queryParams.getQualityinspectionStatus());
            countQuery.setParameter("qualityinspectionStatus", queryParams.getQualityinspectionStatus());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIsValid())) {
            query.setParameter("isValid", queryParams.getIsValid());
            countQuery.setParameter("isValid", queryParams.getIsValid());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDrillCodes())) {
            String codes = queryParams.getDrillCodes();
            List<String> idList = Arrays.asList(codes.split(","));
            query.setParameter("drillCodes", idList);
            countQuery.setParameter("drillCodes", idList);
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<QhSeismicFortificationEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<QhSeismicFortificationEntity> qhSeismicFortificationPage = new PageImpl<>(list, pageable, count.longValue());
        return qhSeismicFortificationPage;
    }
}
